﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net;

namespace Zeus.Log.Log4net
{
    public static class LogHelper
    {
        private static ILog logger;//获取一个记录器

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="message">日志信息</param>
        /// <param name="messageType">日志类型</param>
        public static void WriteLog(this Exception ex, string message, LogMessageType messageType)
        {
            DoLog(message, messageType, ex);
        }

        public static void WriteInfo(string message, LogMessageType messageType)
        {
            DoLogInfo(message, messageType);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="messageType">日志类型</param>
        public static void WriteLog(this Exception ex, LogMessageType messageType)
        {
            DoLog(ex.Message, messageType, ex);
        }
        private static void DoLog(string message, LogMessageType messageType, Exception ex)
        {
            logger = LogManager.GetLogger("loggerAX");
            switch (messageType)
            {
                case LogMessageType.Debug:
                    logger.Debug(message, ex);
                    break;

                case LogMessageType.Info:
                    logger.Info(message, ex);
                    break;

                case LogMessageType.Warn:
                    logger.Warn(message, ex);
                    break;

                case LogMessageType.Error:
                    logger.Error(message, ex);
                    break;

                case LogMessageType.Fatal:
                    logger.Fatal(message, ex);
                    break;
            }
        }
        private static void DoLogInfo(string message, LogMessageType messageType)
        {
            logger = LogManager.GetLogger("loggerInfo");
            switch (messageType)
            {
                case LogMessageType.Debug:
                    logger.Debug(message);
                    break;

                case LogMessageType.Info:
                    logger.Info(message);
                    break;

                case LogMessageType.Warn:
                    logger.Warn(message);
                    break;

                case LogMessageType.Error:
                    logger.Error(message);
                    break;

                case LogMessageType.Fatal:
                    logger.Fatal(message);
                    break;
            }
        }
        public static void writePriceLog(string str)
        {
            logger = LogManager.GetLogger("loggerAX");
            logger.Error(str);
        }
        #region 废
        //private static string _logName = @"E:\NewTravelSolution\Src\CCTTEST\Log\" + DateTime.Now.ToShortDateString() + ".log";
        //private static FileInfo finfo = new FileInfo(_logName);
        //public static string LogName
        //{
        //    get
        //    {
        //        return _logName;
        //    }
        //    set
        //    {
        //        if (!finfo.Exists)
        //            _logName = @"E:\NewTravelSolution\Src\CCTTEST\Log\" + DateTime.Now.ToShortDateString() + ".log";


        //    }
        //}//日志文件地址

        //public static void WriteErrorLog(this Exception ex,string controller, string action)
        //{
        //    if (!string.IsNullOrEmpty(LogName))
        //    {
        //        TextWriter writer = new StreamWriter(LogName, true);
        //        writer.WriteLine("--------------- 开始 ---------------");
        //        writer.WriteLine("Time:[{0}]", DateTime.Now.ToString("yyyy-MM-dd- hh:mm:ss"));
        //        writer.WriteLine("Controller:{0}", controller);
        //        writer.WriteLine("Action:{0}", action);
        //        writer.WriteLine("Class:{0}", ex.Source);
        //        writer.WriteLine("method:{0}", ex.TargetSite);
        //        writer.WriteLine("Message:{0}", ex.Message);
        //        writer.WriteLine("--------------- 结束 ------------------");
        //        writer.Close();

        //    }
        //}
        #endregion

        public static void WriteErrorLog(string log, string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                TextWriter writer = new StreamWriter(path, true);
                writer.WriteLine(log);
                writer.Close();
            }
        }
    }
}
